import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:gold/base/config.dart';
import 'package:gold/http/apis.dart';
import 'package:gold/http/http.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// Descriptions: 剩余积分说明弹窗
/// User: tangzhentao
/// Date: 4:58 下午 2021/12/7
Future<bool?> showRemainPointsDescriptionDialog(int point, int income) async {
  return await showDialog<bool>(
      context: XfgNav.currentContext,
      barrierColor: XfgColor.black.withValues(alpha: 0.66),
      barrierDismissible: false,
      builder: (_) => _RemainPointsDescriptionDialog(point, income),
  );
}

class _RemainPointsDescriptionDialog extends StatelessWidget {
  final int point;
  final int income;

  /// 内容
  final ValueNotifier<String> _htmlNotifier = ValueNotifier('');

  _RemainPointsDescriptionDialog(this.point, this.income) {
    _loadConfig();
  }

  /// 加载配置
  Future _loadConfig() async {
    final result = await Http().request<String>(Apis.config, data: {'key': SystemConfig.pointsRules}, showErrorMsg: false);
    if (result.data?.isNotEmpty == true) {
      _htmlNotifier.value = result.data!;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
            height: XfgSize().size388,
            width: XfgSize().size315,
            decoration: white10Decoration,
            child: Column(
              children: [
                SizedBox(height: XfgSize().size26),
                XfgText('剩余积分说明', fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold),
                Container(
                  margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size26, XfgSize().size10, XfgSize().size16),
                  padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                  height: XfgSize().size36,
                  decoration: BoxDecoration(color: XfgColor.remainPoints, borderRadius: BorderRadius.circular(XfgSize().size5)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      XfgText('剩余积分', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold),
                      XfgText(point.toString(), fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: XfgSize().size20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      XfgText('今日预计到账', fontSize: XfgSize().text14, color: XfgColor.text3),
                      XfgText(income.toString(), fontSize: XfgSize().text14, color: XfgColor.text3),
                    ],
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.all(XfgSize().size20),
                    child: ValueListenableBuilder<String>(
                      valueListenable: _htmlNotifier,
                      builder: (_, html, __) => HtmlWidget(html),
                    )
                  ),
                ),
                strokeHeightContainer,
                InkWell(
                  onTap: () => XfgNav.pop(false),
                  child: SizedBox(
                    height: XfgSize().size67,
                    child: Center(child: XfgText('知道了', color: XfgColor.text1D, fontSize: XfgSize().text18, bold: FontWeight.bold)),
                  ),
                )
              ],
            )
        ),
      ),
    );
  }
}